package com.wl.center.state.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wl.center.model.state.UpdateContent;
import com.wl.center.model.task.User;
import com.wl.center.state.mapper.CustomerMapper;
import com.wl.center.state.mapper.UpdateContentMapper;
import com.wl.center.state.service.CustomerService;
import com.wl.center.state.service.UserService;
import com.wl.center.vo.state.UpdateRecord;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
* @author WangLiang
* @description 针对表【customer】的数据库操作Service实现
* @createDate 2023-02-27 21:48:34
*/
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, User>
    implements CustomerService {
    @Resource
    private UpdateContentMapper updateContentMapper;//爆红不用管
    @Resource
    private UserService userService;
    //导出跟进内容
    @Override
    public void exportUpdateContent(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("跟进内容", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
            List<UpdateContent> updateList = updateContentMapper.selectList(null);
            List<UpdateRecord> updateDtoList = new ArrayList<>(updateList.size());
            for(UpdateContent update : updateList) {
                UpdateRecord updateDto = new UpdateRecord();
                Integer sellerId = update.getUpdatePerson();
                User seller = userService.getById(sellerId);
                String sellerName = seller.getName();
                updateDto.setSetUpdatePersonName(sellerName);
                BeanUtils.copyProperties(update,updateDto);
                updateDtoList.add(updateDto);
            }
            EasyExcel.write(response.getOutputStream(), UpdateRecord.class).sheet("跟进内容").doWrite(updateDtoList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}




